package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduCourse;
import com.zjitc.serviceedu.entity.EduTeacher;
import com.zjitc.serviceedu.entity.vo.TeacherVo;
import com.zjitc.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/serviceedu/edu-teacher")
@Api(tags = "讲师模块")
public class EduTeacherController {
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
    @Autowired
    private EduTeacherService teacherService;
    @ApiOperation("查询所有讲师")
    @GetMapping("TeacherList")
    public RespBean TeacherList(EduTeacher eduTeacher) {
        List<EduTeacher> list = teacherService.list(null);
        return RespBean.success().data("list",list).msg("查询成功");
    }

    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public RespBean addBanner(EduTeacher eduTeacher) {
        boolean result = teacherService.save(eduTeacher);
        if(!result) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }


    @ApiOperation("修改详情查询")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable("id") Long id) {
        EduTeacher teacher = teacherService.getById(id);
        if(teacher == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", teacher).msg("查询成功");
    }

    @ApiOperation("修改讲师")
    @PutMapping("updateTeacher")
    public RespBean updateTeacher(EduTeacher eduTeacher) {
        boolean result = teacherService.updateById(eduTeacher);
        if(!result) {
            throw new AuthException("修改失败");
        }

        return RespBean.success().msg("修改成功");
    }

    @ApiOperation("删除讲师")
    @DeleteMapping("delTeacher/{id}")
    public RespBean delTeacher(@PathVariable("id") String id) {
        boolean result = teacherService.removeById(id);
        if(!result) {
            throw new AuthException("删除失败");
        }

        return RespBean.success().msg("删除成功");
    }

    @ApiOperation("批量删除讲师")
    @DeleteMapping("delMoreTeacher")
    public RespBean delMoreRole(Long[] ids) {
        boolean result = teacherService.removeByIds(Arrays.asList(ids));
        if(!result) {
            throw new AuthException("批量删除失败");
        }

        return RespBean.success().msg("批量删除成功");
    }
    @ApiOperation("条件查询")
    @GetMapping("getPageBatchs")
    public RespBean getPageBatchs(TeacherVo teacherVo) {

        Page<EduTeacher> page = new Page<>(teacherVo.getPage(), teacherVo.getSize());

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(teacherVo.getName())) {
            queryWrapper.like("name", teacherVo.getName());
        }
        if(!StringUtils.isEmpty(teacherVo.getLevel())) {
            queryWrapper.like("level", teacherVo.getLevel());
        }
        if (!StringUtils.isEmpty(teacherVo.getBeginTime())) {
            queryWrapper.ge("gmtCreate", teacherVo.getBeginTime());
        }
        if (!StringUtils.isEmpty(teacherVo.getEndTime())) {
            queryWrapper.le("gmtModified", teacherVo.getEndTime());
        }
        queryWrapper.orderByAsc("id");
        teacherService.page(page, queryWrapper);

        if(page == null) {
            throw new AuthException("查询失败");
        }

        List<EduTeacher> list = page.getRecords();
        Long total = page.getTotal();

        if (list.isEmpty()) {
            // 可以选择返回空列表或抛出异常，这里选择返回空列表和提示信息
            return RespBean.success().data("list", list).data("total", total).msg("未找到符合条件的课程");
        }

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");
    }

}

